const {
  GiftShopModel
} = require('../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
var encryption = require('../../utils/edsEncryption.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderCode: {
      type: String,
      value: '',
      observer: function (newVal, oldVal) {
        this.setData({
          orderCode: newVal
        });
      }
    },
    shopId: {
      type: String,
      value: '',
      observer: function (newVal, oldVal) {
        this.setData({
          shopId: newVal
        });
      }
    },
    passwordShow: {
      type: Boolean,
      value: false,
      observer: function (newVal, oldVal) {
        this.setData({
          passwordShow: newVal
        });
        if (newVal) {
          this.setPayPassword()
        }
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    passwordShow:null,//弹出层
    orderPayInfo: {},
    orderCode: '', //订单号
    shopId: '', //门店id
    isIphoneX: '', //手机型号
    inputPassword: '', //输入的密码
    isMemberPay: null, //是否设置密码 0：未设置 1已设置
    payData: null, //支付成功的信息
  },

  attached() {
    this.getModel()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 获取设备型号
    getModel: function (options) {
      let that = this;
      wx.getSystemInfo({
        success: res => {
          let modelmes = res.model
          if (modelmes.search('iPhone X') != -1 || modelmes.search('iPhone 11') != -1) {
            that.setData({
              isIphoneX: true
            })
          }
        }
      })
    },

    inputPassword(e) {
      let that = this;
      if (that.data.inputPassword.length < 6) {
        //键盘输入的密码 赋值给inputPassword
        that.data.inputPassword = that.data.inputPassword + e.currentTarget.dataset.key;
        that.setData({
          inputPassword: that.data.inputPassword
        });
        //当输入密码正确时   
        if (that.data.inputPassword.length == 6) {
          that.getUserGetKey(); //订单付款
        }
      }
    },
    //删除输入错误的密码
    clear() {
      var index = this.data.inputPassword.length;
      if (index > 0) {
        var inputPassword = this.data.inputPassword.substr(0, index - 1);
        this.setData({
          inputPassword: inputPassword
        });
      }
    },

    /**
     * 发起支付验证密码
     */
    setPayPassword() {
      giftShopModel.balanceOrderTopay({
        merchantId: wx.getStorageSync('merchantId'), //商户ID
        shopId: this.data.shopId, //门店id
        orderCode: this.data.orderCode //订单号
      }).then(data => {
        this.setData({
          isMemberPay: data.isMemberPay,
          orderPayInfo: data
        })
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    },
    /**
     * 动态获取加密密钥
     */
    getUserGetKey: function () {
      let that = this;
      giftShopModel.userGetKey({
        merchantId: wx.getStorageSync('merchantId'), //商户ID
        k: that.data.orderCode, //订单号
      }).then(data => {
        that.getUserOrderPay(data.secretKey)
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    },

    /**
     * 订单付款
     */
    getUserOrderPay: function (secretKey) {
      let that = this;
      giftShopModel.userOrderPay({
        merchantId: wx.getStorageSync('merchantId'), //商户ID
        orderCode: that.data.orderCode, //订单号
        shopId: that.data.shopId, //门店ID
        password: encryption.encryptByDES(that.data.inputPassword, secretKey, secretKey) //用户密码
      }).then(data => {
        that.setData({
          payData: data
        })
        that.triggerEvent('orderpay');
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
        if (err.errorCode == "20001") {
          that.setData({
            inputPassword: ''
          });
        }
      })
    },

    /**
     * 关闭按钮
     */
    goPayment: function (e) {
      let that = this;
      let type = e.currentTarget.dataset.type;
      if (type == 2) {
        wx.showModal({
          title: '是否放弃付款？',
          cancelColor: '#333333', //取消文字的颜色
          confirmColor: '#333333', //确定文字的颜色
          cancelText: '取消付款',
          confirmText: '继续付款',
          success(res) {
            if (res.confirm) { //确定
            } else if (res.cancel) { //取消
              that.triggerEvent('close');
            }
          }
        })
      } else if (type == 1) {
        that.triggerEvent('close');
      }
    },

    /**
     * 修改密码
     */
    goPassword: function (e) {
      this.setData({
        passwordShow: false
      });
      wx.navigateTo({
        url: '/pages/giftShop/password/phoneVerify/index?pwdType=0'
      })
    },
  }
})